package com.yxr.affairs;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.yxr.affairs.base.BaseActivity1;
import com.yxr.affairs.utils.SPUtils;
import com.yxr.affairs.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;


public class AddActivity extends BaseActivity1 {


    @BindView(R.id.ll_top_view)
    LinearLayout llTopView;
    @BindView(R.id.user_feedback_goback)
    ImageButton userFeedbackGoback;
    @BindView(R.id.ip)
    EditText ip;
    @BindView(R.id.d_ip)
    EditText dIp;
    @BindView(R.id.login_sign_in)
    Button loginSignIn;
    @BindView(R.id.add_code)
    LinearLayout addCode;

    private String IP = null;
    private String DIP = null;

    @Override
    protected int attachLayoutRes() {

        return R.layout.add_activity;
    }

    @Override
    protected void initViews() {

        userFeedbackGoback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        if (SPUtils.getInstance(mContext).contains("mdata")) {
            if (!TextUtils.isEmpty(SPUtils.getInstance(mContext).getString("mdata"))) {


                String str = SPUtils.getInstance(mContext).getString("mdata");
                ip.setText( str.substring(0, str.indexOf(":")));
                dIp.setText(str.substring(str.indexOf(":")+1));

                Log.d("AAAA",SPUtils.getInstance(mContext).getString("mdata"));
                 /*       Log.d("AAAAA",IP.toString());

                        ip.setText(IP);
                        dIp.setText(DIP);*/
            }
        }else {
            ip.setText("10.1.55.16");
            dIp.setText("8050");
        }

        loginSignIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                IP = ip.getText().toString();
                DIP = dIp.getText().toString();

                if(!TextUtils.isEmpty(IP)&&!TextUtils.isEmpty(DIP)){
                    getShow();
                }else {
                    ToastUtil.toast(mContext,"请填写ip地址");
                }

            }
        });
    }

    private void getShow() {

        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setTitle("提示");
        builder.setMessage("确定更改ip地址为"+"\n"+"http://"+ IP+":"+DIP+"更改成功后，会跳转到登陆页面重新登录");
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();

            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                if (!TextUtils.isEmpty(ip.getText())&&!TextUtils.isEmpty(dIp.getText())) {
                    IP = ip.getText().toString();
                    DIP = dIp.getText().toString();
                    SPUtils.getInstance(mContext).put("mdata", IP+":"+DIP);
                }
                finish();
                EventBus.getDefault().post(new Piye());
                startActivity(new Intent(mContext, LoginActicity1.class));
                overridePendingTransition(R.anim.slide_right_entry, R.anim.hold);

                }
        });
        builder.create().show();



    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    @Override
    protected void onError_(Throwable e) {

    }

    @Override
    protected void onCancel_() {

    }



}
